function fields = jcb_xmlreader(filename,figurenumber)

pathname='';
ext='';
if nargin<1
    [filelist pathname] = jcb_pick_files('*.xml','pick an xml file to plot',false);
else
    if exist(filename,'file')
        [pathname filename ext] = fileparts(filename);
        filelist = {[filename ext]};
    end
    if ~exist(fullfile(pathname,[filename ext]),'file') || isempty(ext)
        if exist([filename '.xml'],'file')
            [pathname filename ext] = fileparts([filename '.xml']);
            filelist = {[filename ext]};
        else
            error('jcb:BadFileName','couldn''t find the file ''%s'' or ''%s.xml''',filename,filename)
        end
    end    
end

filename=filelist{1};

try
    document=xmlread(fullfile(pathname,filelist{1}));
catch
    error('Failed to read XML file %s.',fullfile(pathname,filelist{1}));
end

header   = document.getElementsByTagName('header').item(0);
biginlib = header.getElementsByTagName('biginlib').item(0);
libname = biginlib.getElementsByTagName('libname').item(0);
units = biginlib.getElementsByTagName('units').item(0);
fieldlist = biginlib.getElementsByTagName('bgnstr');
% we only go to fieldlist.getLength-1 since the last one is the 'pattern'
% one which doesn't contain structures
allocCell=cell(fieldlist.getLength-1,1); 
fields=struct('strname',allocCell,'shapes',allocCell,'extremes',allocCell);
extremes = Inf*[1 1; -1 -1];



fieldlayout=fieldlist.item(fieldlist.getLength-1);
scale=1000;
for ii=1:length(fields)
    fields(ii)=makeFieldStruct(fieldlist.item(ii-1));
    extremes(1,:)=min(extremes(1,:),fields(ii).extremes(1,:));
    extremes(2,:)=max(extremes(2,:),fields(ii).extremes(2,:));
    for jj=1:fieldlayout.getElementsByTagName('sref').getLength
        fld=fieldlayout.getElementsByTagName('sref').item(jj-1);
        fldname=strtrim(char(fld.getElementsByTagName('sname').item(0).getTextContent));
        if strcmp(fldname,fields(ii).strname)>0
            textline=[' ' char(fld.getElementsByTagName('xy').item(0).getTextContent)];
            c = regexp(textline,...
                    '[\s\>]((-?\d*(\.\d)?)),((-?\d*(\.\d)?))','tokens');
            x=str2double(c{1}{1})/scale;
            y=str2double(c{1}{2})/scale;
            fields(ii).location=struct('x',x,'y',y);
        end
    end
end





minvals=extremes(1,:);
maxvals=extremes(2,:);

ranges=maxvals-minvals;

if sum(ranges>600)
    fieldsize=[];
elseif sum(ranges>120)
    fieldsize=600;
elseif sum(ranges>60)
    fieldsize=120;
else
    fieldsize=[];
end

if nargin < 2
    figurenumber=clf;    
    title(['layout of ' strrep(filename,'_','\_')]);
end
figure(figurenumber);
axis equal
xlabel('X/\mum');
ylabel('Y/\mum');

for ii=1:length(fields)
    for jj=1:length(fields(ii).shapes)
         patch(fields(ii).shapes(jj).x,fields(ii).shapes(jj).y,0.8*[1 1 1]);
    end
end

xlim auto;
ylim auto;
%lims = [xlim; ylim];
%xlim(range(lims(1,:))*[-0.1 0.1]+lims(1,:));
%ylim(range(lims(2,:))*[-0.1 0.1]+lims(2,:));

if ~isempty(fieldsize)
    field.x = minvals(1)+[0 0 1 1 0]*fieldsize;
    field.y = maxvals(2)-[0 1 1 0 0]*fieldsize;
    line( field.x,field.y,'color','r');
end

if nargout<1
    clear fields
end

end

function fieldStruct = makeFieldStruct(fieldNode,scale)
    if nargin<2
        scale=1000;
    end
    allocCell=cell(fieldNode.getElementsByTagName('xy').getLength,1);
    shapelist = struct('x',allocCell,'y',allocCell);
    extremes = Inf*[1 1; -1 -1];
    for ii=1:length(shapelist)
        textline=[' ' char(fieldNode.getElementsByTagName('xy').item(ii-1).getTextContent)];
        c = regexp(textline,...
                    '[\s\>]((-?\d*(\.\d)?)),((-?\d*(\.\d)?))','tokens');
        shapelist(ii)=struct('x',zeros(length(c)-1,1),'y',zeros(length(c)-1,1));
        for jj=1:length(c)-1    %-1 since last coord is a repeat of the first
            x=str2double(c{jj}{1})/scale;
            y=str2double(c{jj}{2})/scale;
            shapelist(ii).x(jj)=x;
            shapelist(ii).y(jj)=y;
            extremes(1,:)=min(extremes(1,:),[x y]);
            extremes(2,:)=max(extremes(2,:),[x y]);
        end
    end
    fieldStruct = struct('strname',strtrim(char(fieldNode.getElementsByTagName('strname').item(0).getTextContent)),...
                      'shapes',shapelist,'extremes',extremes);
end